package com.zh.webflux.config;

import java.util.Collection;

import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

/**
 * 分表策略
 * 
 * @author zhongh2
 *
 */
public class DemoTableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

	/**
	 * @param availableTargetNames
	 *            可用表名
	 * 
	 * @param shardingValue
	 *            分片条件
	 * @return 符合分片条件的表名
	 */
	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
		for (String each : availableTargetNames) {
			System.out.println("table:" + each);
			// shardingValue.getValue() 分片规则的值
			if (each.endsWith(Long.parseLong(shardingValue.getValue().toString()) % 2 + "")) {
				return each;
			}
		}
		throw new IllegalArgumentException();
	}

}
